package com.mcxx.modules.minimumliving.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.minimumliving.service.SalaryRecoveryService;
import com.mcxx.modules.miniSalvation.family.dto.SalvationFamilyDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundRecoverDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundRecoverEntity;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundRecoverReadService;
import com.mcxx.modules.miniSalvation.salary.param.SalvationRecoveryParam;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.*;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestService("minimumliving/fundpay/recovery/v2")
@Slf4j
@Validated
@Api("追回救助金管理")
public class SalaryRecoveryController extends DefaultRestService {
    @Autowired
    private SalaryRecoveryService salaryRecoveryService;
    @Autowired
    private FundRecoverReadService fundRecoverReadService;

    @PostMapping("getSalvationRecoveryFamilyList")
    @ApiOperation("查询救助家庭列表")
    public void getSalvationRecoveryFamilyList(SalvationRecoveryParam param, PageParam pageParam) {
        log.info(String.format("查询救助家庭列表，请求参数：{%s} 分页 :{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        Page<SalvationFamilyDTO> page = salaryRecoveryService.getSalvationRecoveryFamilyList(param, pageParam, getCurUserAccount());
        setPageBean(page);
    }

    @PostMapping("getRecoveryList")
    @ApiOperation("查询已登记追回救助金列表")
    public void getRecoveryList(SalvationRecoveryParam param, PageParam pageParam) {
        log.info(String.format("查询已登记追回救助金列表，请求参数：{%s} 分页 :{%s}", JSON.toJSONString(param),
                JSON.toJSONString(pageParam)));
        Page<FundRecoverDTO> page = salaryRecoveryService.getRecoveryList(param, pageParam, getCurUserAccount());
        setPageBean(page);
    }

    @PostMapping("addSalaryRecovery")
    @ApiOperation("登记追回救助金信息")
    public void addSalaryRecovery(@V({"notnull"}) FundRecoverEntity param) {
        log.info("登记追回救助金信息{}", JSON.toJSONString(param));
        salaryRecoveryService.addSalaryRecovery(param,getCurUserAccount());
        setData(true);
    }

    @PostMapping("modifySalaryRecovery")
    @ApiOperation("修改追回救助金信息")
    public void modifySalaryRecovery(FundRecoverEntity param) {
        log.info("修改追回救助金信息" + JSON.toJSONString(param));
        if (CommonUtil.invalidArgs(param.getId())) {
            throw new BizException(FailureCode.ERR_10000);
        }
        param.setStartDate(param.getStartDate().replaceAll("-", ""));
        int result = salaryRecoveryService.modifySalaryRecovery(param);
        if (result < 1) {
            setError(FailureCode.ERR_30006.getMsg());
        }
        setData(result);
    }

    @PostMapping("queryRecoveryById")
    @ApiOperation("根据id查询")
    public void queryRecoveryById(String id) {
        log.info("根据id查询" + id);



        setData(fundRecoverReadService.getById(id));
    }

    @PostMapping("cancelSalaryRecovery")
    @ApiOperation("撤销追回救助金信息")
    public void cancelSalaryRecovery(String ids) {
        log.info("撤销追回救助金信息" + ids);
        if (CommonUtil.invalidArgs(ids)) {
            throw new BizException(FailureCode.ERR_10000);
        }
        int result = salaryRecoveryService.cancelSalaryRecovery(ids);
        if (result < 1) {
            setError(FailureCode.ERR_30006.getMsg());
        }
        setData(result);
    }

    @PostMapping("confirmSalaryRecovery")
    @ApiOperation("确认追回救助金信息")
    public void confirmSalaryRecovery(String id) {
        log.info("确认追回救助金信息" + id);
        if (CommonUtil.invalidArgs(id)) {
            throw new BizException(FailureCode.ERR_10000);
        }
        int result = salaryRecoveryService.confirmSalaryRecovery(id);
        if (result < 1) {
            setError(FailureCode.ERR_30006.getMsg());
        }
        setData(result);
    }

    @PostMapping("delSalaryRecovery")
    @ApiOperation("删除追回救助金信息")
    public void delSalaryRecovery(String ids) {
        log.info("删除追回救助金信息" + ids);
        if (CommonUtil.invalidArgs(ids)) {
            throw new BizException(FailureCode.ERR_10000);
        }
        int result = salaryRecoveryService.delSalaryRecovery(ids);
        if (result < 1) {
            setError(FailureCode.ERR_10024.getMsg());
        }
        setData(result);
    }

    @PostMapping("downLoadSalaryRecovery")
    @ApiOperation(value="导出追回救助金列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出临时救助乡镇待办信息")
    public void downLoadTownSalvation(HttpServletResponse response, @RequestBody SalvationRecoveryParam param)throws IOException {
        log.info("开始导出追回救助金列表，参数是：{}", param);
        ExcelInfo excelInfo = salaryRecoveryService.exportExcelSave(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        log.info("开始导出导出追回救助金列表，查询结果是：{}", excelInfo);
    }

    @PostMapping("canConfirmSalaryRecovery")
    @ApiOperation("能否确认追回救助金信息")
    public void canConfirmSalaryRecovery(String id) {
        log.info("能否确认追回救助金信息，参数{}" + id);
        if (CommonUtil.invalidArgs(id)) {
            throw new BizException(FailureCode.ERR_10000);
        }
        int i = salaryRecoveryService.canConfirmSalaryRecovery(id);
        setData(i);
        log.info("能否确认追回救助金信息完成，结果{}" + i);
    }
    @PostMapping("queryRecoveryInfo")
    @ApiOperation("查询追回救助金详情")
    public void queryRecoveryInfo(String id) {
        log.info("查询追回救助金详情，参数{}" + id);
        if (CommonUtil.invalidArgs(id)) {
            throw new BizException(FailureCode.ERR_10000);
        }
        FundRecoverDTO dto = salaryRecoveryService.queryRecoveryInfo(id);
        setData(dto);
        log.info("查询追回救助金详情完成，结果{}" + dto);
    }
}
